// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    searchValue: '',
    // 非遗旅游标签页状态
    tourActiveTab: 'routes',
    // 旅游路线数据
    tourRoutes: [
      { id: 1, title: '此生必驾318 在雅精彩', views: '80', imageUrl: '/static/images/tour1.svg' },
      { id: 2, title: '雨城区非遗之旅', views: '180', imageUrl: '/static/images/tour2.svg' },
      { id: 3, title: '荣经县非遗之旅', views: '88', imageUrl: '/static/images/tour3.svg' },
      { id: 4, title: '天全县非遗之旅', views: '158', imageUrl: '/static/images/tour4.svg' }
    ],
    // 旅游资讯数据
    tourNews: [
      { id: 1, title: '雅安非遗旅游线路推荐', views: '120', imageUrl: '/static/images/tour-news1.svg' },
      { id: 2, title: '非遗+旅游 文化传承创新', views: '95', imageUrl: '/static/images/tour-news2.svg' }
    ],
    // 轮播图数据
    bannerList: [
      { id: 1, imageUrl: '/static/images/banner1.jpg' },
      { id: 2, imageUrl: '/static/images/banner2.jpg' },
      { id: 3, imageUrl: '/static/images/banner3.jpg' },
    ],
    navItems: [
      { icon: 'records', text: '非遗资源', url: '/pages/heritage/index' },
      { icon: 'video', text: '非遗视频', url: '/pages/video/index' },
      { icon: 'shop', text: '非遗展馆', url: '/pages/museum/index' },
      { icon: 'logistics', text: '非遗旅游', url: '/pages/tour/index' },
      { icon: 'shopping-cart', text: '非遗购物', url: '/pages/shop/index' }
    ],
    // 视频数据
    videoList: [
      { id: 1, title: '非遗里的雅安-荣经砂器烧制技艺', coverUrl: '/static/images/video1.svg', views: '2.3万', source: '雅安文化馆' },
      { id: 2, title: '非遗里的雅安-饶镇多声部民歌', coverUrl: '/static/images/video2.svg', views: '1.8万', source: '雅安文化馆' },
      { id: 3, title: '非遗里的雅安-雅安藤编技艺', coverUrl: '/static/images/video3.svg', views: '1.5万', source: '雅安文化馆' }
    ],
    // 页面数据
    currentPage: 1,
    // 标签页状态
    activeTab: 'projects',
    // 非遗项目数据
    heritageProjects: [
      { id: 1, title: '饶镇多声部民歌', date: '2024-04-23', views: '127', imageUrl: '/static/images/heritage1.svg' },
      { id: 2, title: '荣经砂器烧制技艺', date: '2024-04-23', views: '103', imageUrl: '/static/images/heritage2.svg' }
    ],
    // 非遗传承人数据
    heritageInheritors: [
      { id: 1, name: '张跃华', avatar: '/static/images/inheritor1.svg', skill: '传统技艺', area: '名山区' },
      { id: 2, name: '曾庆红', avatar: '/static/images/inheritor2.svg', skill: '传统技艺', area: '荣经县' },
      { id: 3, name: '甘玉祥', avatar: '/static/images/inheritor3.svg', skill: '传统技艺', area: '雨城区' },
      { id: 4, name: '杨明星', avatar: '/static/images/inheritor4.svg', skill: '传统音乐', area: '宝兴县' }
    ],
    // 非遗购物数据
    shops: [
      { id: 1, name: '雅安藤编工艺品店', imageUrl: '/static/images/shop1.svg' },
      { id: 2, name: '荥经砂器文创馆', imageUrl: '/static/images/shop2.svg' },
      { id: 3, name: '天全竹艺精品店', imageUrl: '/static/images/shop3.svg' },
      { id: 4, name: '芦山漆器专卖店', imageUrl: '/static/images/shop4.svg' }
    ],

    // 非遗工坊数据
    workshops: [
      { id: 1, title: '天全县中医医院', address: '四川省/雅安市/天全县/城厢镇广建南路29号', views: '258', imageUrl: '/static/images/workshop1.svg' },
      { id: 2, title: '四川蒙顶山跃华茶业集团有限公司', address: '四川省/雅安市/名山区/蒙顶山大道560号', views: '186', imageUrl: '/static/images/workshop2.svg' },
      { id: 3, title: '荥经县朱氏砂器有限公司', address: '四川省/雅安市/荥经县/严道街道古城村四组', views: '142', imageUrl: '/static/images/workshop3.svg' },
      { id: 4, title: '宝兴县彦青雕塑艺术有限公司', address: '四川省/雅安市/宝兴县/河口村原河口小学', views: '198', imageUrl: '/static/images/workshop4.svg' }
    ],
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  
  // 搜索框输入变化
  onSearchChange(e) {
    this.setData({
      searchValue: e.detail
    })
  },
  
  // 搜索提交
  onSearch() {
    const { searchValue } = this.data
    // 这里可以实现搜索逻辑
    console.log('搜索内容:', searchValue)
    wx.showToast({
      title: `搜索: ${searchValue}`,
      icon: 'none'
    })
  },
  
  // 切换标签页
  switchTab(e) {
    const tab = e.currentTarget.dataset.tab
    this.setData({
      activeTab: tab
    })
  },

  // 切换旅游标签页
  switchTourTab(e) {
    const tab = e.currentTarget.dataset.tab
    this.setData({
      tourActiveTab: tab
    })
  },

  // 展馆卡片点击事件
  onMuseumTap(e) {
    const type = e.currentTarget.dataset.type
    if (type === 'info') {
      wx.navigateTo({
        url: '/pages/museum/index'
      })
    } else if (type === 'news') {
      wx.navigateTo({
        url: '/pages/museum/news'
      })
    }
  },

  // 导航项点击事件
  onNavItemTap(e) {
    const text = e.currentTarget.dataset.text
    if (text === '非遗资源') {
      wx.navigateTo({
        url: '/pages/heritage/index'
      })
    } else if (text === '非遗视频') {
      wx.navigateTo({
        url: '/pages/video/index'
      })
    } else if (text === '非遗展馆') {
      wx.navigateTo({
        url: '/pages/museum/index'
      })
    } else if (text === '非遗旅游') {
      wx.navigateTo({
        url: '/pages/tour/index'
      })
    } else if (text === '非遗购物') {
      wx.navigateTo({
        url: '/pages/shop/index'
      })
    }
  },

  // 点击非遗资源更多
  onHeritageMore() {
    wx.navigateTo({
      url: '/pages/heritage/index'
    })
  },

  // 点击非遗视频更多
  onVideoMore() {
    wx.navigateTo({
      url: '/pages/video/index'
    })
  },

  // 点击非遗旅游更多
  onTourMore() {
    wx.navigateTo({
      url: '/pages/tour/index'
    })
  },

  // 点击非遗展馆更多
  onMuseumMore() {
    wx.navigateTo({
      url: '/pages/museum/index'
    })
  },

  // 点击非遗工坊更多
  onWorkshopMore() {
    wx.navigateTo({
      url: '/pages/workshop/index'
    })
  },

  // 点击非遗购物更多
  onShopMore() {
    wx.navigateTo({
      url: '/pages/shop/index'
    })
  }

})
